#!/usr/bin/env python

"""
        __author__      = "Vishal Patil"
        __copyright__   = "Copyright (C) 2006 Vishal Patil"
"""

#
#	Base class for the chromosone. All the new chromosones need to be derived from
#	this class. The important functions that need to implemented in the derived
#	class are clone and fitness
#

class Chromosone:

	#
	#	Simply initialize the list of genes
	#	
	def __init__(self):
		self.genes = list()

	def getGenes(self):
		return self.genes;

	def setGenes(self,genes):
		self.genes = genes

	def addGene(self,gene):
		self.genes.append(gene)	
	
	#
	#	Mutate each of the gene of the chromosone
	#
	def mutate(self):
		for gene in self.genes:
			gene.mutate()		
	
	#
	#	Determines the fitness of the chromosone. Lower the value better the 
	#	fitness of the chromosone. It is important to follow this paradigm
	#	for the framework to behave correctly. If you need a reverse behavior
	#	i.e. higher the value greater the fitness be sure to overide the
	#	__cmp__ function for the the derive chromosone as well. 
	#
	def fitness(self):
		pass

	#
	#	Create a clone of this chromosone. Make sure that the new chromosone
	#	has cloned all of it's genes. Look at the Cubic solver and TSP 
	#	implementations for examples
	#
	def clone(self):
		pass

	#
	#	Initialize each of the member gene to a random value
	#
	def setRandomValue(self):
		for gene in self.genes:
			gene.setRandomValue()

	#
	#	Compares the fittness of this chromosone with that of the other. Return
	#	1 if this chromosone is fitter than the other chromosone. In case the 
	#	other chromosone is fitter return -1 and in case both are equally fit
	#	return 0 
	#	
	def __cmp__(self,other):
		ret = 0
		if self.fitness() > other.fitness():
			ret = 1
		elif self.fitness() < other.fitness():
			ret = -1 

		return ret			 
